// <copyright file="PageExtensions.cs" company="Clever Software">
// Copyright (c) 2013, 2014 All Right Reserved, http://www.cleversoftware.by
//
// This source is subject to the Microsoft Permissive License.
// Please see the License.txt file for more information.
// All other rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// </copyright>
// <author>Clever Software</author>
// <email>info@cleversoftware.by</email>
// <date>2014-02-04</date> 
// <summary>CleverCMS source code</summary>

namespace CleverCMS.Core.Web.Extensions
{
    using System;
    using System.Web.Mvc;
    using System.Web.Mvc.Html;
    
    using Cms.Data.Entities;

    public static class PageExtensions
    {
        public static MvcHtmlString ActionPageLink(this HtmlHelper hh, CmsPage p)
        {
            return hh.ActionLink(p.PageTitle, "Content", "Home",
                                 new { id = p.Id, name = p.Alias, area = "" },
                                 new { title = p.PageTitle });
        }

        public static MvcHtmlString PageLink(this HtmlHelper helper, CmsPage page, object htmlAttributes)
        {
            return PageLink(helper, null, page, htmlAttributes);
        }

        public static MvcHtmlString PageLink(this HtmlHelper helper, string linkText, CmsPage page, object htmlAttributes)
        {
            return helper.ActionLink(linkText ?? page.PageTitle, "Content", "Home", new {area = "", id = page.Id, name = page.Alias}, htmlAttributes);
        }

        public static string PageUrl(this UrlHelper helper, CmsPage page)
        {
            var context = helper.RequestContext.HttpContext;
            return Utils.GetBaseUrl(context.Request.ApplicationPath, context.Request.Url, false) +  helper.Action("Content", "Home", new {area = "", id = page.Id, name = page.Alias});
        }

        public static string SiteUrl(this UrlHelper helper)
        {
            var context = helper.RequestContext.HttpContext;
            return Utils.GetBaseUrl(context.Request.ApplicationPath, context.Request.Url, true);
        }

        public static string PageLink(this HtmlHelper helper, CmsPage page)
        {
            if (null != page)
                return page.GetUrl();

            return string.Empty;
        }

        public static bool IsDefaultPage(this HtmlHelper helper)
        {
            return
                "Home".Equals(helper.ViewContext.RouteData.GetRequiredString("controller"), StringComparison.InvariantCultureIgnoreCase) &&
                "Default".Equals(helper.ViewContext.RouteData.GetRequiredString("action"), StringComparison.InvariantCultureIgnoreCase);
        }
    }
}